package com.century.sjt.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.century.sjt.R;
import com.century.sjt.EMChat.activity.ChatActivity;
import com.century.sjt.EMChat.fragment.ChatFragment;
import com.century.sjt.EMChat.fragment.EMBaseFragment;
import com.century.sjt.EMChat.fragment.EaseChatFragment;

//import com.century.sjt.EMChat.ui.EaseChatFragment;


/**
 * 商家消息
 */
public class MerchantMessageFragment extends EMBaseFragment {
    public static ChatActivity activityInstance;
    private EaseChatFragment chatFragment;
    String toChatUsername;
    private Context mContext;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.sjt_merchant_message_fragment, container, false);

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mContext=getActivity();
        activityInstance = new ChatActivity();
        //聊天人或群id
        toChatUsername =getActivity().getIntent().getExtras().getString("merchantId");
        toChatUsername="18650495431";
        Intent intent = new Intent();
        intent.putExtra("userId", toChatUsername);
        intent.putExtra("toChatType","1");

        //可以直接new EaseChatFratFragment使用
        chatFragment = new ChatFragment();
        //传入参数
        chatFragment.setArguments(intent.getExtras());
        getActivity().getSupportFragmentManager().beginTransaction().add(R.id.container, chatFragment).commit();
        initViews();

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void setUpView() {

    }

    private void initViews() {


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        activityInstance = null;
    }


    protected void onNewIntent(Intent intent) {
//         点击notification bar进入聊天页面，保证只有一个聊天页面
        String username = intent.getStringExtra("userId");
        if (toChatUsername.equals(username))
            this.onNewIntent(intent);
        else {
            activityInstance.finish();
            mContext.startActivity(intent);
        }

    }
    public void onBackPressed() {
        chatFragment.onBackPressed();
    }

    public String getToChatUsername(){
        return toChatUsername;
    }



}
